package com.hotelsystem.reservations.rooms;

import java.util.ArrayList;
import java.util.Date;

import com.google.gson.Gson;
import com.hotelsystem.reservations.reservations.Reservation;

public class Room {

	private int roomNumber;
	private RoomTypesEnum roomType;
	private boolean isSmokersFree;
	private String imageFilePath;

	public Room() {
		super();
	}

	public Room(int roomNumber, RoomTypesEnum roomType, boolean isSmokersFree) {
		super();
		this.roomNumber = roomNumber;
		this.roomType = roomType;
		this.isSmokersFree = isSmokersFree;
	}
	
	public Room(int roomNumber, RoomTypesEnum roomType, boolean isSmokersFree, String imageFilePath) {
		super();
		this.roomNumber = roomNumber;
		this.roomType = roomType;
		this.isSmokersFree = isSmokersFree;
		this.imageFilePath = imageFilePath;
	}

	public int getRoomNumber() {
		return roomNumber;
	}

	public String getImageFilePath() {
		return imageFilePath;
	}

	public void setImageFilePath(String imageFilePath) {
		this.imageFilePath = imageFilePath;
	}
	
	public void setRoomNumber(int roomNumber) {
		this.roomNumber = roomNumber;
	}

	public RoomTypesEnum getRoomType() {
		return roomType;
	}

	public void setRoomType(RoomTypesEnum roomType) {
		this.roomType = roomType;
	}

	public boolean isSmokersFree() {
		return isSmokersFree;
	}

	public void setSmokersFree(boolean isSmokersFree) {
		this.isSmokersFree = isSmokersFree;
	}

	public boolean isRoomFree(Date startDate, Date endDate, ArrayList<Reservation> reservations) {

		boolean isFree = false;
		boolean roomHasReservations = false;
		if (reservations.isEmpty()) {
			return true;
		}

		for (Reservation reservation : reservations) {
			if (roomNumber == reservation.getRoom().getRoomNumber()) {
				roomHasReservations = true;
				if (endDate.before(reservation.getStartDate()) || (startDate.after(reservation.getEndDate()))) {
					isFree = true;
				}
			}
		}
		
		if(!roomHasReservations)
		{
			isFree = true;
		}
		
		return isFree;
	}
	
	public void fromJson(String json){
		Gson gson = new Gson();				    
		Room tmp = gson.fromJson(json, this.getClass());
		roomNumber = tmp.roomNumber;
		roomType = tmp.roomType;
		isSmokersFree = tmp.isSmokersFree;	
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Room)) {
			return false;
		}

		Room lhs = (Room) obj;

		return (this.roomNumber == lhs.roomNumber && this.roomType.equals(lhs.roomType)
				&& this.isSmokersFree == lhs.isSmokersFree);
	}

	@Override
	public String toString() {
		return "Room [roomNumber=" + roomNumber + ", roomType=" + roomType + ", isSmokersFree=" + isSmokersFree + "]";
	}

}
